package LinkedList_03.Single_Linked_List_03.LinkedList_new_01;

/*
 * @Author 罗
 * @date 2020/3/16 - 6:57 下午
 *
 */

public class LinkedListSingle <E extends Integer>{
    /**
     * 静态内部类用来存储节点数据
     * @param <E>
     */
    private static class Node<E extends Integer> {
        E item;
        LinkedListSingle.Node<E> next;

        /**
         * 构造器
         * @param element   存储本节点
         * @param next      指向下一个节点
         */
        Node(E element, LinkedListSingle.Node<E> next) {
            this.item = element;
            this.next = next;
        }

        Node(E item) {
            this.item = item;
        }

        public Node() {
        }

        @Override
        public String toString() {
            return "Node{" +
                    "item=" + item +
                    '}';
        }
    }

    Node<E> head;





    /**
     * 外部类的构造器
     */
    public LinkedListSingle() {
        this.head = new Node(0);
    }

    public void add(E e){
        Node temp = this.head;
        while(true){
            if(temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = new Node(e);
    }

    public void addOrderly(Integer e){
        Node temp = head;
        while(true){
            if(temp.next == null){
                break;
            }
            if(temp.next.item.intValue() > e.intValue()){
                break;
            }
            temp = temp.next;
        }
        Node node = new Node(e);
        node.next = temp.next;
        temp.next = node;
    }

    public int get(int i){
        Node temp = head;
        for (int j = 0; j < i; j++) {
            temp = temp.next;
        }
        return temp.item;
    }

    public void show(){
        Node temp = head.next;
        while(true){
            if(temp == null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
    public int size(){
        Node temp = head.next;
        int count = 0;
        while(true){
            if(temp == null){
                break;
            }
            temp = temp.next;
            count++;
        }
        return count;
    }
}
